#include "WINApt_Hdr.h"

LPVOID WINApt_Thread_Packet(LPVOID lParam)
{
    while (bIsRun)
    {
        if (!HelpComponents_Datas_WaitEventEx(xhTCPPacket))
        {
            continue;
        }
        WINAPT_THREADPOOL *pSt_PoolTask = (WINAPT_THREADPOOL *)malloc(sizeof(WINAPT_THREADPOOL));
        if (NULL == pSt_PoolTask)
        {
            XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("申请内存失败，错误：%d"),errno);
            continue;
        }
        memset(pSt_PoolTask,'\0',sizeof(WINAPT_THREADPOOL));
        pSt_PoolTask->nMsgLen = sizeof(pSt_PoolTask->tszMsgBuffer);

        if (!HelpComponents_Datas_GetRandomEx(xhTCPPacket,pSt_PoolTask->tszClientAddr,pSt_PoolTask->tszMsgBuffer,&pSt_PoolTask->nMsgLen,&pSt_PoolTask->st_ProtocolHdr))
        {
            DWORD dwRet = Packets_GetLastError();
            if ((ERROR_HELPCOMPONENTS_PACKETS_PROTOCOL_GET_ISNULL != dwRet) && (ERROR_HELPCOMPONENTS_PACKETS_PROTOCOL_GET_NOCOMPLETE != dwRet))
            {
                XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("获取一个随机包失败，错误：%lX"),dwRet);
            }
            free(pSt_PoolTask);
            pSt_PoolTask = NULL;
            continue;
        }
        ManagePool_Thread_PostTask(xhPool,WINApt_Thread_Pool,pSt_PoolTask);
    }
    return NULL;
}

LPVOID WINApt_Thread_Pool(LPVOID lParam)
{
    WINAPT_THREADPOOL *pSt_PoolTask = (WINAPT_THREADPOOL *)lParam;

    int nSDLen = 204800;
    TCHAR tszSDBuffer[nSDLen];
    memset(tszSDBuffer,'\0',nSDLen);

    if (ENUM_NETENGINE_WINAPT_PROTOCOL_TYPE_SEARCH == pSt_PoolTask->st_ProtocolHdr.unOperatorType)
    {
        if (NETENGINE_WINAPT_PROTOCOL_CODE_SEARCH_REQUEST == pSt_PoolTask->st_ProtocolHdr.unOperatorCode)
        {
            TCHAR tszSQLQuery[1024];
            list<LOGICSQL_SOFTLIST> stl_SoftList;
            memset(tszSQLQuery,'\0',sizeof(tszSQLQuery));

            if (!LogicProtocol_QueryPacket_FileReq(pSt_PoolTask->tszMsgBuffer,tszSQLQuery))
            {
                pSt_PoolTask->st_ProtocolHdr.unOperatorCode = NETENGINE_WINAPT_PROTOCOL_CODE_SEARCH_RESPONSE;
                LogicProtocol_CommPacket_HdrJson(&pSt_PoolTask->st_ProtocolHdr,tszSDBuffer,&nSDLen,-1,"查询失败，查询格式错误！");
                WINApt_SendMsg_Client(pSt_PoolTask->tszClientAddr,tszSDBuffer,nSDLen);
                XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("客户端：%s，请求查询库失败，查询格式错误"),pSt_PoolTask->tszClientAddr);
                goto TASKEXIST;
            }
            if (!LogicSql_DevRoot_GetAll(tszSQLQuery,&stl_SoftList))
            {
                pSt_PoolTask->st_ProtocolHdr.unOperatorCode = NETENGINE_WINAPT_PROTOCOL_CODE_SEARCH_RESPONSE;
                LogicProtocol_CommPacket_HdrJson(&pSt_PoolTask->st_ProtocolHdr,tszSDBuffer,&nSDLen,-2,"查询失败，没有找到数据");
                WINApt_SendMsg_Client(pSt_PoolTask->tszClientAddr,tszSDBuffer,nSDLen);
                XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR,_T("客户端：%s，请求查询库:%s，失败，错误码：%lX"),pSt_PoolTask->tszClientAddr,tszSQLQuery,LogicSql_GetLastError());
                goto TASKEXIST;
            }
            LogicProtocol_QueryPacket_FileRep(&pSt_PoolTask->st_ProtocolHdr,&stl_SoftList,tszSDBuffer,&nSDLen);
            WINApt_SendMsg_Client(pSt_PoolTask->tszClientAddr,tszSDBuffer,nSDLen);
            XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO,_T("客户端：%s，请求查询库:%s，成功，查询到的结果个数：%d"),pSt_PoolTask->tszClientAddr,tszSQLQuery,stl_SoftList.size());
            stl_SoftList.clear();
        }
        else
        {
            XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN,_T("客户端：%s，发送的协议码不支持：%X"),pSt_PoolTask->tszClientAddr,pSt_PoolTask->st_ProtocolHdr.unOperatorCode);
        }
    }
    else
    {
        XLOG_PRINT(xhLog,NETENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN,_T("客户端：%s，发送的协议类型不支持：%X"),pSt_PoolTask->tszClientAddr,pSt_PoolTask->st_ProtocolHdr.unOperatorType);
    }

TASKEXIST:
    free(pSt_PoolTask);
    pSt_PoolTask = NULL;
    return NULL;
}
